<?php
/**
 * Ly - 系统项目运行类
 *
 * 提供项目运行功能，如路由调度，插件调用，应用实例化
 *
 * Copyright (c) 2013 YinHailin All rights reserved.
 * @Author: YinHailin
 * @Authorize: Boren Network
 * @Created by YinHailin at 2013-11-01
 */

defined('LY_VERSION') or exit();

class AppCore {

  /* 项目运行 */
  static public function Start() {
    //路由器调度
    RouterCore::Router();
    //系统变量安全过滤
    if (C('VAR_FILTERS')) {
      $filters = explode(',', C('VAR_FILTERS'));
      foreach ($filters as $filter) {
        //全局参数过滤
        array_walk_recursive($_POST, $filter);
        array_walk_recursive($_GET, $filter);
      }
    }
    if (defined('PLUGIN_NAME')) {
      //插件调用则创建插件实例
      if (!preg_match('/^[A-Za-z](\w)*$/', PLUGIN_NAME)) {
        $plugin = false;
      } else {
        $plugin = A(PLUGIN_NAME.'#//'.PLUGIN_NAME);
        if (class_exists(PLUGIN_NAME) && method_exists(PLUGIN_NAME, ACTION_NAME)) {
          call_user_func_array(array($plugin, ACTION_NAME), array());
        } else {
          halt('调用的插件方法：'.PLUGIN_NAME.'::'.ACTION_NAME.'不存在', __FILE__, __LINE__);
        }
      }
    } else {
      //创建模块实例
      if (!preg_match('/^[A-Za-z](\w)*$/', MODULE_NAME)) {
        $module = false;
      } else {
        $class = MODULE_NAME.'Mod';
        $module = A($class);
        if (class_exists($class) && method_exists($class, ACTION_NAME)) {
          call_user_func_array(array($module, ACTION_NAME), array());
        } else {
          halt(MODULE_NAME.'::'.ACTION_NAME.'不存在', __FILE__, __LINE__);
        }
      }
    }
  }
}